/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.toto.observable;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author Administrator
 */
public class ShowDigit extends JPanel implements Observer
{
    Observable subject;
    JTextArea text;
    Vector<String> vector;
    ShowDigit(Observable subject)
    {
        this.subject = subject;
        subject.addObserver(this);
        text = new JTextArea(10, 15);
        text.setLineWrap(true);
        text.setWrapStyleWord(true);
        text.setEditable(false);
        add(new JScrollPane(text));
        vector = new Vector<String>();
    }
    public void update(Observable subject, Object object)
    {
        text.setText(null);
        text.append("出现的数字有：\n");
        vector.removeAllElements();
        String content = object.toString();
        String regex = "\\D+";
        String digiWords[] = content.split(regex);
        for (int i = 0; i < digiWords.length; i++)
        {
            if (! vector.contains(digiWords[i]))
            {
                vector.add(digiWords[i]);
            }
        }
        for (int i= 0; i < vector.size(); i++)
        {
            text.append(vector.elementAt(i) + " ");
        }
    }
}
